package com.wm.cmsService.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wm.cmsService.entity.CrmBanner;
import com.wm.cmsService.service.CrmBannerService;
import com.wm.commonutils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 首页banner表 前端控制器
 * </p>
 *
 * @author sshdg
 * @since 2020-10-07
 */
@RestController
//@CrossOrigin
@Api(tags = "幻灯片后台")
@RequestMapping("/cmsService/bannerAdmin")
public class BannerAdminController {
    @Autowired
    CrmBannerService bannerService;


    @ApiOperation("分页查询")
    @GetMapping("pageBanner/{current}/{limit}")
    public R pageListTeacher(@PathVariable long current,
                             @PathVariable long limit){
        Page<CrmBanner> page = new Page<>(current, limit);
        bannerService.page(page);
        // 总记录数
        long total = page.getTotal();
        // 分页查询的数据
        List<CrmBanner> records = page.getRecords();
        return R.ok().data("total", total).data("rows", records);
    }

    @ApiOperation("添加")
    @PostMapping("/addBanner")
    public R addTeacher(@RequestBody CrmBanner banner){
        boolean save = bannerService.save(banner);
        if (save){
            return R.ok();
        } else {
            return R.error();
        }
    }
    @ApiOperation("修改")
    @PutMapping("/updateBanner")
    public R updateTeacher(@RequestBody CrmBanner banner){

        boolean updateResult = bannerService.updateById(banner);

        if(updateResult){
            return R.ok();
        }else {
            return R.error();
        }
    }

    @DeleteMapping("deleteBanner/{id}")
    @ApiOperation("逻辑删除")
    public R removeTeacher(@PathVariable String id){
        boolean result = bannerService.removeById(id);
        if (result){
            return R.ok();
        } else {
            return R.error();
        }
    }

    @GetMapping("/getBanner/{id}")
    @ApiOperation("查询")
    public R getTeacher(@PathVariable("id") String id){
        CrmBanner banner = bannerService.getById(id);
        return R.ok().data("banner",banner);
    }
}
